<template>
  <div>
    <el-card>
      <template #header>
        <div style="display: flex; justify-content: space-between">
          <el-button @click="lastPage"> 返回上一级</el-button>
          <el-button @click="addFo" type="primary"
            >新增<el-icon style="margin-left: 5px"><Plus /></el-icon>
          </el-button>
        </div>
      </template>
      <el-table
        ref="enterpriseRef"
        :data="tableData"
        style="width: 100%; max-height: 61vh; height: 61vh"
        table-layout="fixed"
        v-loading="loading"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column prop="formulaId" label="公式id" />
        <el-table-column prop="formulaName" label="公式名" />
        <el-table-column prop="formulaWeight" label="权重" />
        <el-table-column fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="edit(scope.$index, scope.row)"
              >编辑</el-button
            >

            <el-popover
              placement="top-start"
              title="删除操作"
              :width="200"
              trigger="click"
              :visible="visibleSS[scope.$index]"
            >
              <p>确定要进行删除吗?</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="small"
                  text
                  @click="visibleSS[scope.$index] = false"
                  >取消</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  @click="deltef(scope.$index, scope.row)"
                  >确定</el-button
                >
              </div>
              <template #reference>
                <el-button
                  link
                  type="danger"
                  ref="buttonRefs"
                  @click="visibleSS[scope.$index] = true"
                  >删除</el-button
                >
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      
      <formulaEdit></formulaEdit>
    </el-card>
  </div>
</template>

<script setup>
import mitts from "@/utils/bus.js";
import {
  selectB
} from "@/api/enterpriseArchives";
import { selectA ,del} from "@/api/formulaConfiguration";
import { datacollectconfigure } from "@/api/enterprise";
import { reactive, ref, onBeforeMount } from "vue";
import { ElMessage } from "element-plus";
import formulaEdit from "@/views/enterpriseArchives/device/edit/formula/index.vue";
const buttonRefs = ref([]);
const visibleSS = ref([]);
const code = ref("");
const loading = ref(true);
const tableData = ref([]);
const topOptions = ref([]);
const updateData = reactive({
  enterpriseCode: "", //社会信用代码
  dataCode: "", //采集数据项编码
  operator: "1", //运算符
  deviceIdList: "", //参与运算的设备 "23,24,25,26,33"
  formulaId: "", //引用公式id "21,31"
  id: "", //公式id，空为新增公式
});
const temp = reactive({
  enterpriseCode:""
})

//直接设备参数选项
const deviceOption = ref([]);
//统计参数选项
const formulaOption = ref([]);
onBeforeMount(() => {
  mitts.on("enterpriseCode", (e) => {
    code.value = e;
    temp.enterpriseCode = e;
    doDataList(temp);
  });
});

/**
 * 新增公式
 */
const addFo = () => {
  mitts.emit("formulaEdit", {
    topOptions: topOptions.value,
    deviceOption: deviceOption.value,
    formulaOption: formulaOption.value,
    dataId: "",
  });
};
mitts.on("formulaColse", (e) => {
  if(e==true){
    doDataList(temp);
  }
})

const lastPage = () => {
  mitts.emit("isShow", false);
};

const doDataList = (data) => {
  loading.value = true;
  let datat = {
    pageNum: "1",
    pageSize: "100",
    condition: JSON.stringify([
      { column: "enterpriseCode", type: "eq", value: code.value },
    ]),
  };
  datacollectconfigure(datat)
    .then((res) => {
      topOptions.value = res.data.records;
    })
    .catch((msg) => {
      console.log(msg);
      //获取出错
      ElMessage.error("获取信息失败，请联系管理员！");
    });
  selectA(data)
    .then((res) => {
      //请求完成,开始处理数据
      tableData.value = res.data;
      loading.value = false;
      formulaOption.value = res.data;
    })
    .catch((msg) => {
      console.log(msg);
      //获取出错
      ElMessage.error("获取信息失败，请联系管理员！");
    });
  //设备options
  selectB(data)
    .then((res) => {
      deviceOption.value = res.data;
    })
    .catch((msg) => {
      console.log(msg);
      //获取出错
      ElMessage.error("获取信息失败，请联系管理员！");
    });
};

/**
 * 编辑公式
 * @param {*} index
 * @param {*} rowData
 */
const edit = (index, rowData) => {
  mitts.emit("formulaEdit", {
    topOptions: topOptions.value,
    deviceOption: deviceOption.value,
    formulaOption: formulaOption.value,
    dataId: rowData.formulaId,
  });
  
};

const deltef = (index, rowData) => {
  loading.value = true;
  let params = {
    dataId: rowData.formulaId,
  };
  del(params)
    .then((res) => {
      if (res.status == 200) {
        ElMessage.success("删除成功！");
        visibleSS.value[index] = false;
        
        selectA(temp)
          .then((res) => {
            //请求完成,开始处理数据
            tableData.value = res.data;
            loading.value = false;
            formulaOption.value = res.data;
          })
          .catch((msg) => {
            console.log(msg);
            //获取出错
            ElMessage.error("获取信息失败，请联系管理员！");
          });
      }else{
        ElMessage.error(res.message);
        loading.value = false;
      }
    })
    .catch((msg) => {});
  // 显示popover
  visibleSS.value[index] = false;
};

</script>

<style lang="less" scoped></style>
